package com.yunxi.common.config;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisCacheService {
	private static final Logger logger = LoggerFactory.getLogger(RedisCacheService.class);

	@Autowired
	private RedisTemplate redisTemplate;
	
	private static String redisPrefixKey= "";

	public boolean set(String key, Object value, long seconds) {
		boolean result = false;
		try {
			String redisAllKey=redisPrefixKey+key;
			RedisSerializer<String> redisSerializer = new StringRedisSerializer();
			redisTemplate.setKeySerializer(redisSerializer);

			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
			operations.set(redisAllKey, value);

			redisTemplate.expire(redisAllKey, seconds, TimeUnit.SECONDS);

			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public boolean set(String key, Object value) {
		boolean result = false;
		try {
			String redisAllKey=redisPrefixKey+key;
			RedisSerializer<String> redisSerializer = new StringRedisSerializer();
			redisTemplate.setKeySerializer(redisSerializer);

			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
			operations.set(redisAllKey, value);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public boolean delete(String key) {
		boolean result = false;
		try {
			String redisAllKey=redisPrefixKey+key;
			if (redisTemplate.hasKey(redisAllKey))
				redisTemplate.delete(redisAllKey);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public Object get(String key) {
		Object result = null;
		try {
			String redisAllKey=redisPrefixKey+key;
			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
			result = operations.get(redisAllKey);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

	public String getString(String key) {
		String result = null;
		try {
			String redisAllKey=redisPrefixKey+key;
			ValueOperations<Serializable, String> operations = redisTemplate.opsForValue();
			result = operations.get(redisAllKey);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

	public Object getObject(String key,Class cls) {
		Object result = null;
		try {
			String redisAllKey=redisPrefixKey+key;
			ValueOperations<Serializable, String> operations = redisTemplate.opsForValue();
			String aaa = operations.get(redisAllKey);
			return JSONObject.parseObject(aaa,cls);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

	public List getList(String content, Class cls) {
		String result = "[]";
		try {
			if(content.startsWith("\"")) content = content.substring(1,content.length()-1);
			result = StringEscapeUtils.unescapeEcmaScript(content);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return JSONObject.parseArray(result,cls);
	}

	public boolean hasKey(String key) {
		boolean result = false;
		try {
			String redisAllKey=redisPrefixKey+key;
			result = redisTemplate.hasKey(redisAllKey);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}
	
	public boolean deleteKeys(String key) {
		boolean result = false;
		try {
			String redisAllKey=redisPrefixKey+key;
			Set<String> keys = redisTemplate.keys(redisAllKey + "*");
			if(keys.size()>0)
			{
				redisTemplate.delete(keys);	
			}
			result = true;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}
}
